var app = angular.module('app', ['ngCookies']);
//var _debug;

app.directive('format', ['$filter', function($filter) {
    return {
        require: '?ngModel',
        link: function(scope, elem, attrs, ctrl) {
            if (!ctrl) return;

            //model -> view 
            ctrl.$formatters.unshift(function(a) {
              return $filter(attrs.format)(ctrl.$modelValue)
            });
            
            //view -> model
            ctrl.$parsers.unshift(function(viewValue) {
              var plainNumber = viewValue.replace(/[^\d|\-+|\.+]/g, '');
              elem.val( $filter('number')(plainNumber) );
              return plainNumber;
            });
        }
    };
}]);

app.factory('PaymentMethods', function() {
  return {
      KEREN_SHAVA     : "1",
      SHPITZER        : "2",
      PARTIAL_GRACE   : "3",
      FULL_GRACE      : "4",
      PARTIAL_BALOON  : "5",
      FULL_BALOON     : "6",
  };
});

app.factory('MortageTypes', function() {
  return {
      PRIME                 : "1",
      MISHTANA_TSMUDA_2_5   : "2",
      MISHTANA_TSMUDA_5     : "3",
      MISHTANA_TSMUDA_7     : "4",
      MISHTANA_TSMUDA_10    : "5",
      MISHTANA_LO_TSMUDA_2_5: "6",
      MISHTANA_LO_TSMUDA_5  : "7",
      MISHTANA_LO_TSMUDA_7  : "8",
      MISHTANA_LO_TSMUDA_10 : "9",
      KVUA_TSMUDA           : "10",
      KVUA_LO_TSMUDA        : "11",
      MAKAM                 : "12",
      DOLAR                 : "13",
      EURO                  : "14",
      MISHTANA_TSMUDA       : "98",
      MISHTANA_LO_TSMUDA    : "99",
  };
});

app.factory('PrimeInterestForecast', function() {
  var ret = {};
  ret.lowArr = [
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.75
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.75
    +0.00, +0.00, +0.00, -0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.75
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
  ];

  ret.midArr = [
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.75
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.75
    +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, // 0.00
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.00, // 0.25
    +0.00, +0.00, +0.25, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, // 0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.25, +0.00, // 0.25
    +0.00, +0.25, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, // 0.25
    +0.00, +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, // 0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
  ];

  ret.highArr = [
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.75
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.75
    +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, // 0.00
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.25, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, // 0.00
    +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.00, +0.00, +0.00, +0.00, //+0.25
  ];

  ret.customArr = [
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.75
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.75
    +0.00, +0.00, +0.00, -0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.75
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, +0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, +0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //+0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
    +0.00, +0.00, +0.00, +0.25, +0.00, +0.00, -0.25, +0.00, -0.25, +0.00, +0.00, +0.00, //-0.25
  ];

  ret.addToArr = function(arr, base){
    var res = [];
    var length = arr.length;
    res[0] = base + arr[0];
    for (var i = 1 ; i < length ; i++){
      res[i] = res[i-1] + arr[i];
    }
    return res;
  };

  ret.low = function(base){
    return this.addToArr(this.lowArr, base);
  };

  ret.mid = function(base){
    return this.addToArr(this.midArr, base);
  },
  ret.high = function(base){
    return this.addToArr(this.highArr, base);
  };
  ret.custom = function(base){
    return this.addToArr(this.customArr, base);
  };

  ret.setSelectedForecastMethod = function(arr){
    this.changes = arr;
  }

  ret.changes = [
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//+0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//+0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00, 0.25, 0.00, 0.00, 0.00,//+0.75
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//+0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//+0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00,-0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//-0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//+0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//+0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//+0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00, 0.25, 0.00, 0.00, 0.00,//+0.75
    0.00, 0.00, 0.00,-0.25, 0.00, 0.00,-0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//-0.75
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//+0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//+0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00, 0.25, 0.00, 0.00, 0.00,//+0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00, 0.25, 0.00, 0.00, 0.00,//+0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00,-0.25, 0.00, 0.25, 0.00, 0.00, 0.00,//+0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00,-0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//-0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00,-0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//-0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00,-0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//-0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00,-0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//-0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00,-0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//-0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//+0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//+0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00, 0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//+0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00,-0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//-0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00,-0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//-0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00,-0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//-0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00,-0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//-0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00,-0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//-0.25
    0.00, 0.00, 0.00, 0.25, 0.00, 0.00,-0.25, 0.00,-0.25, 0.00, 0.00, 0.00,//-0.25
  ];

  ret.getPrime = function(basePrime, fromMonth, toMonth){
    if(fromMonth < 0 || fromMonth > this.changes.length || toMonth < 0 || toMonth > this.changes.length){
      console.log("illegal value for month: " + fromMonth + " or " + toMonth + " legal values are 0 to: " + this.changes.length);
      return basePrime;
    }
    for(var i = fromMonth ; i < toMonth ; i++){
      basePrime += this.changes[i];
    }
    return basePrime;
  }

  ret.getPrimeChange = function(month, isPrime){
    if(isPrime == false){
      return 0;
    }else{
      return this.getPrime(0, month, month+1);
    }
  }
  return ret;
});

app.factory('RouteCalculator', function(PaymentMethods, MortageTypes, PrimeInterestForecast) { 
  var ret = {};

/*  ret.calcFixedLinkedMonthly = function(sum, payments, interest, prime, inflation){
    var payPerMonth = [];
    var monthlyPay = sum / payments;
    var total = 0;
    prime /= 100;
    interest /= 100;
    for (var i = 0 ; i < payments ; i ++){
      payPerMonth.push(monthlyPay);
      total += monthlyPay;
      if((i + 1) % 12 == 0) {
        monthlyPay *= 1 + (interest + prime);
      }
    }
    return ({
      total : total,
      payments : payPerMonth,
    });
  };*/

  ret.calcKerenShava = function(sum, payments, interest, accumulatedInflationInterest, madadMonthly, accumulatedPrimeInterest, isPrime, changeYears, paidPayments){
    console.log("started calcKerenShava\n from " + paidPayments + "\n with interst " + interest + "\n accumulatedPrimeInterest " + accumulatedPrimeInterest + "\n accumulatedInflationInterest " + accumulatedInflationInterest);
    var total = 0;
    var payPerMonth = [];
    var kerenPerMonth = [];
    var monthlyPay = 0;
    // A/T+R*(A-(A/T)*i)
    //var A = sum;//var T = payments;
    var R = interest / 1200;
    var changeMonths = changeYears ? changeYears * 12 : payments;
    var kerenPm = sum/payments;
    for(var i = 1 ; i <= payments && i <= changeMonths; i++){
      accumulatedInflationInterest *= madadMonthly;
      accumulatedPrimeInterest *= (1 + (PrimeInterestForecast.getPrimeChange(i-1, isPrime) / 100));
      monthlyPay = (sum/payments+R*(sum-(sum/payments)*i)) * accumulatedInflationInterest * accumulatedPrimeInterest;
      payPerMonth.push(monthlyPay);
      kerenPerMonth.push(kerenPm);
      total += monthlyPay;
    }
    // checking if there is a need to recalculate when change period arrived
    if(changeMonths < payments){
      // calling it self with less months / payments
      var res = this.calcKerenShava(sum - (changeMonths * kerenPm), //updating the new sum (unpaid load = total keren - payments done to the keren)
                                    payments - changeMonths, 
                                    PrimeInterestForecast.getPrime(interest, paidPayments, paidPayments + changeMonths),
                                    accumulatedInflationInterest, 
                                    madadMonthly, 
                                    accumulatedPrimeInterest, 
                                    isPrime, 
                                    changeYears,
                                    paidPayments + changeMonths);
      total += res.total;
      payPerMonth = payPerMonth.concat(res.payments);
      kerenPerMonth = kerenPerMonth.concat(res.kerenPayments);
    }

    return {total    : total,
            payments : payPerMonth,
            kerenPayments    : kerenPerMonth};
  }

  ret.calcShpitzer = function(sum, payments, interest, accumulatedInflationInterest, madadMonthly, accumulatedPrimeInterest, isPrime, changeYears, paidPayments){
    console.log("started calcShpitzer\n from " + paidPayments + "\n with interst " + interest + "\n accumulatedPrimeInterest " + accumulatedPrimeInterest + "\n accumulatedInflationInterest " + accumulatedInflationInterest);
    var total = 0;
    var payPerMonth = [];
    var kerenPerMonth = [];
    var monthlyPay = 0;
    var changeMonths = changeYears ? changeYears * 12 : payments;
    var paidFromKeren = 0;

    monthlyPay = Math.abs(PMT1(interest, 12, payments, sum, 0));
    //total = total + (monthlyPay * payments);
    for (var i = 0 ; i < payments && i < changeMonths ; i ++){
      accumulatedInflationInterest *= madadMonthly;
      accumulatedPrimeInterest *= (1 + (PrimeInterestForecast.getPrimeChange(i-1, isPrime) / 100));
      var thisMonthPayment = monthlyPay * accumulatedInflationInterest * accumulatedPrimeInterest;
      var kerenPm = Math.abs(PPMT(interest/1200, i+1, payments, sum));
      payPerMonth.push(thisMonthPayment);
      kerenPerMonth.push(kerenPm);
      paidFromKeren += kerenPm;
      total += thisMonthPayment;
    }

    // checking if there is a need to recalculate when change period arrived
    if(changeMonths < payments){
      // calling it self with less months / payments
      var res = this.calcShpitzer(sum - paidFromKeren, 
                                  payments - changeMonths, 
                                  PrimeInterestForecast.getPrime(interest, paidPayments, paidPayments + changeMonths),
                                  accumulatedInflationInterest, 
                                  madadMonthly, 
                                  accumulatedPrimeInterest, 
                                  isPrime, 
                                  changeYears,
                                  paidPayments + changeMonths);
      total += res.total;
      payPerMonth = payPerMonth.concat(res.payments);
      kerenPerMonth = kerenPerMonth.concat(res.kerenPayments);
    }

    return {total    : total,
            payments : payPerMonth,
            kerenPayments    : kerenPerMonth};
  }

  ret.processRoute = function(route, sum, payments, interest, prime, inflation, paymentMethod, gracePeriod, changeYears){
    /*var rawInterest = prime + interest;
    var interest = (rawInterest / 100) + 1;
    var total = sum;
    var years = payments / 12;
    var payPerMonth = [];
    total = sum * Math.pow( 1+(rawInterest/1200), payments);
    var tempPpm = total / payments;
    for (var i = 0 ; i < payments ; i++){
      payPerMonth.push(tempPpm);
    }*/
    //isKerenShava = true;
    var monthlyPay;
    var total = 0;
    var payPerMonth = [];
    var kerenPerMonth = [];
    var madadMonthly = 1; // starting from neutral value
    var accumulatedInflationInterest = 1; // will be multiplied by the 'madadMonthly' for each month
    var accumulatedPrimeInterest = 1;
    var isPrime = false;
    var changeMonths = changeYears ? changeYears * 12 : payments;

    if(route == MortageTypes.KVUA_TSMUDA || route == MortageTypes.MISHTANA_TSMUDA){
      var madadMonthly = Math.pow( (1+(inflation/100)), 1/12 ); // from yearly to monthly interest
      //inflationEffect = Math.pow(Mm,payments/12); //* ((Math.pow(Mm,payments/12) -1) / (Mm - 1));
      //sum *= inflationEffect;
    }
    if(route == MortageTypes.PRIME || route == MortageTypes.MAKAM){
      isPrime = true;
      interest = prime + interest; // the interest is usually prime minus somehting (e.g., current prime '-' 0.9)
    }

    // Grace types
    if((paymentMethod == PaymentMethods.FULL_GRACE || 
        paymentMethod == PaymentMethods.PARTIAL_GRACE) && 
       gracePeriod > 0){
      //graceIntPerMonth = sum * (interest/1200);
      // multiplying the sum in the power of the interest per year
      var additionalSum = (sum * Math.pow(1+(interest/100), gracePeriod / 12)) - sum;
      var graceIntPerMonth = additionalSum / gracePeriod;
      if(paymentMethod == PaymentMethods.FULL_GRACE){
        for (var i = 0 ; i < gracePeriod ; i ++){
          payPerMonth.push(0);
          kerenPerMonth.push(0);
        }
        sum += additionalSum;
      }else if(paymentMethod == PaymentMethods.PARTIAL_GRACE){
        for (var i = 0 ; i < gracePeriod ; i ++){
          payPerMonth.push(graceIntPerMonth);
          kerenPerMonth.push(0);
        }
        total += additionalSum;
      }
      paymentMethod = PaymentMethods.SHPITZER;
    }
  
    // Shpitzer / Keren Shava
    if(paymentMethod == PaymentMethods.KEREN_SHAVA){
      var res = this.calcKerenShava(sum, payments, interest, accumulatedInflationInterest, madadMonthly, accumulatedPrimeInterest, isPrime, changeYears, 0);
      total += res.total;
      payPerMonth = payPerMonth.concat(res.payments);
      kerenPerMonth = kerenPerMonth.concat(res.kerenPayments);

    }else if(paymentMethod == PaymentMethods.SHPITZER){
      var res = this.calcShpitzer(sum, payments, interest, accumulatedInflationInterest, madadMonthly, accumulatedPrimeInterest, isPrime, changeYears, 0);
      total += res.total;
      payPerMonth = payPerMonth.concat(res.payments);
      kerenPerMonth = kerenPerMonth.concat(res.kerenPayments);

    // Baloon types
    }else if((paymentMethod == PaymentMethods.FULL_BALOON) || (paymentMethod == PaymentMethods.PARTIAL_BALOON)){
      //graceIntPerMonth = sum * (interest/1200);
      var additionalSum = (sum * Math.pow(1+(interest/100), payments / 12)) - sum;
      var graceIntPerMonth = additionalSum / payments;
      if(paymentMethod == PaymentMethods.FULL_BALOON){
        for (var i = 0 ; i < payments ; i ++){
          payPerMonth.push(0);
          kerenPerMonth.push(0);
          //sum += graceIntPerMonth;
        }
        sum += additionalSum;
      }else if(paymentMethod == PaymentMethods.PARTIAL_BALOON){
        for (var i = 0 ; i < payments ; i ++){
          payPerMonth.push(graceIntPerMonth);
          kerenPerMonth.push(0);
          //total += graceIntPerMonth;
        }
        total += additionalSum;
      }
      payPerMonth.push(sum);
      kerenPerMonth.push(sum);
      total += sum;
    }
    

    /*for(var i = 0 ; i < years ; i++){
      var gap;
      var thisMonthPayment;
      var oldTotal = total;
      // checking if it is a partial year
      if(i+1 > years){
        total *= (((years - i) * rawInterest) / 100) + 1;
        gap = total - oldTotal;
        thisMonthPayment = Math.round((((sum / years)*(years-i)) + gap) / (12*(years-i)));
      }else{
        total *= interest;
        gap = total - oldTotal;
        thisMonthPayment = Math.round(((sum / years) + gap) / 12);
      }
      //var gap = total - oldTotal;
      //var thisMonthPayment = Math.round(((sum / years) + gap) / 12);
      for(var j = i*12 ; j < payments && j < (i+1)*12 ; j++){
        payPerMonth.push(thisMonthPayment);
      }
    }*/
    return ({
      loan : sum,
      total : total,
      payments : payPerMonth,
      kerenPayments : kerenPerMonth,
    });
  };


  //calcMonthlyByRoute : function(route, sum, payments, interest, prime, inflation){
  ret.calcMonthlyByRoute = function(selected, prime, inflation){
    /*this.routeMonthly = {
      "1" : this.calcFixedLinkedMonthly,
      "2" : this.calcPrimeMonthly,
      "3" : this.calcVarA1Monthly,
      "4" : this.calcVarA2_5Monthly,
      "5" : this.calcVarA5Monthly,
      "6" : this.calcVarA7Monthly,
      "7" : this.calcVarA10Monthly,
      "8" : this.calcVarB1Monthly,
    };*/
    var route       = selected.track,
        sum         = selected.sum,
        payments    = selected.period * 12,
        interest    = selected.interest,
        payMethod   = selected.payMethod,
        gracePeriod = selected.graceLen,
        changeYears = 0;

    switch(route){
      case MortageTypes.MISHTANA_TSMUDA_2_5 :
        route = MortageTypes.MISHTANA_TSMUDA;
        changeYears = 2.5;
        break;
      case MortageTypes.MISHTANA_TSMUDA_5 :
        route = MortageTypes.MISHTANA_TSMUDA;
        changeYears = 5;
        break;
      case MortageTypes.MISHTANA_TSMUDA_7 :
        route = MortageTypes.MISHTANA_TSMUDA;
        changeYears = 7;
        break;
      case MortageTypes.MISHTANA_TSMUDA_10 :
        route = MortageTypes.MISHTANA_TSMUDA;
        changeYears = 10;
        break;
      case MortageTypes.MISHTANA_LO_TSMUDA_2_5:
        route = MortageTypes.MISHTANA_LO_TSMUDA;
        changeYears = 2.5;
        break;
      case MortageTypes.MISHTANA_LO_TSMUDA_5:
        route = MortageTypes.MISHTANA_LO_TSMUDA;
        changeYears = 5;
        break;
      case MortageTypes.MISHTANA_LO_TSMUDA_7:
        route = MortageTypes.MISHTANA_LO_TSMUDA;
        changeYears = 7;
        break;
      case MortageTypes.MISHTANA_LO_TSMUDA_10:
        route = MortageTypes.MISHTANA_LO_TSMUDA;
        changeYears = 10;
        break;
    }
    /*if(undefined == sum      ||
       undefined == route    ||
       undefined == payments ||
       undefined == interest){
      return {};
    }*/
    //return this.routeMonthly[route](sum, payments, interest, prime, inflation);
    return this.processRoute(route, sum, payments, interest, prime, inflation, payMethod, gracePeriod, changeYears);
  };

  return ret;
});

function Ctrl($scope, $cookieStore, RouteCalculator, MortageTypes, PrimeInterestForecast) {
    //_debug = $scope;
    var primeInterestForecast = {};
    $scope.init = function() {
      $scope.initCookies(primeInterestForecast);
      $scope.showPrimeSpeculations = false;
      $scope.showOur = true;
      $scope.showCur = true;
      $scope.startPrime = primeInterestForecast.startPrime;
      $scope.PrimeInterestForecast = {};
      $scope.PrimeInterestForecast.low = primeInterestForecast.lowArr;
      //$scope.PrimeInterestForecast.lowCalc = PrimeInterestForecast.low($scope.startPrime);
      $scope.PrimeInterestForecast.mid = primeInterestForecast.midArr;
      //$scope.PrimeInterestForecast.midCalc = PrimeInterestForecast.mid($scope.startPrime);
      $scope.PrimeInterestForecast.high = primeInterestForecast.highArr;
      //$scope.PrimeInterestForecast.highCalc = PrimeInterestForecast.high($scope.startPrime);
      $scope.PrimeInterestForecast.custom = primeInterestForecast.customArr;
      //$scope.PrimeInterestForecast.customCalc = PrimeInterestForecast.custom($scope.startPrime);
      // initializing all calculated arrays of prime interest forecast
      $scope.updateCalcArr("prime");
      //$scope.showCompare = false;
      $scope.showCompare = true;
      $scope.compareSpecMonth = 0;
      $scope.curMidSum = 0;
      $scope.ourMidSum = 0;
      $scope.clearOldData();
      
      $scope.ourTracks = [
      /*{
        sum : 100,
        period : 4,
        interest : 0.5,
        monthly : 1000,
        total : 1000000,
      }*/];
      $scope.addRowOurProposal();
      $scope.proposedTracks = [];
      $scope.addRowCurrentProposal();
      $scope.primeInterestForecastSelect = [
      {
        name : "צפי נמוך 2% - 5.5%",
        value : "low",
      },
      {
        name : "צפי סביר",
        value : "mid",
      },
      {
        name : "צפי גבוה",
        value : "high",
      },
      {
        name : "הזנה ידנית",
        value : "custom",
      },
      {
        name : "עריכה...",
        value : "edit",
      }];

      $scope.selectedPrimeForecastMethod = $scope.primeInterestForecastSelect[1].value;
      $scope.tracks = [{
          name  : "פריים",
          index : MortageTypes.PRIME,
        },
        {
          name  : "משתנה צמודה 2.5",
          index : MortageTypes.MISHTANA_TSMUDA_2_5,
        },
        {
          name  : "משתנה צמודה 5",
          index : MortageTypes.MISHTANA_TSMUDA_5,
        },
        {
          name  : "משתנה צמודה 7",
          index : MortageTypes.MISHTANA_TSMUDA_7,
        },
        {
          name  : "משתנה צמודה 10",
          index : MortageTypes.MISHTANA_TSMUDA_10,
        },
        {
          name  : "משתנה לא צמודה 2.5",
          index : MortageTypes.MISHTANA_LO_TSMUDA_2_5,
        },
        {
          name  : "משתנה לא צמודה 5",
          index : MortageTypes.MISHTANA_LO_TSMUDA_5,
        },
        {
          name  : "משתנה לא צמודה 7",
          index : MortageTypes.MISHTANA_LO_TSMUDA_7,
        },
        {
          name  : "משתנה לא צמודה 10",
          index : MortageTypes.MISHTANA_LO_TSMUDA_10,
        },
        {
          name  : "קבועה צמודה",
          index : MortageTypes.KVUA_TSMUDA,
        },
        {
          name  : "קבועה לא צמודה",
          index : MortageTypes.KVUA_LO_TSMUDA,
        },
        {
          name  : "מק\"מ",
          index : MortageTypes.MAKAM,
        },
        {
          name  : "דולר",
          index : MortageTypes.DOLAR,
        },
        {
          name  : "אירו",
          index : MortageTypes.EURO,
        }];
    };

    $scope.initCookies = function(container){
      container.startPrime = $cookieStore.get('primeInterestForecast.startPrime');
      container.lowArr     = $cookieStore.get('primeInterestForecast.lowArr');
      container.midArr     = $cookieStore.get('primeInterestForecast.midArr');
      container.highArr    = $cookieStore.get('primeInterestForecast.highArr');
      container.customArr  = $cookieStore.get('primeInterestForecast.customArr');

      if(!container.startPrime){
        //alert("no startPrime");
        container.startPrime = 2.0;
        $cookieStore.put('primeInterestForecast.startPrime', container.startPrime);
      }  
      if(!container.lowArr){
        //alert("no lowArr");
        container.lowArr = PrimeInterestForecast.lowArr;
        $cookieStore.put('primeInterestForecast.lowArr', container.lowArr);
      }
      if(!container.midArr){
        //alert("no midArr");
        container.midArr = PrimeInterestForecast.midArr;
        $cookieStore.put('primeInterestForecast.midArr', container.midArr);
      }
      if(!container.highArr){
        //alert("no highArr");
        container.highArr = PrimeInterestForecast.highArr;
        $cookieStore.put('primeInterestForecast.highArr', container.highArr);
      }
      if(!container.customArr){
        //alert("no customArr");
        container.customArr = PrimeInterestForecast.customArr;
        $cookieStore.put('primeInterestForecast.customArr', container.customArr);
      } 
      //console.log("Got container: " + JSON.stringify(container));
    }

    $scope.updateCalcArr = function(name){
      if(name != "prime"){
        $scope.compareProposals();
      }
      if(name == "low"){
        $scope.PrimeInterestForecast.lowCalc = PrimeInterestForecast.addToArr($scope.PrimeInterestForecast.low, $scope.startPrime);
        $cookieStore.put('primeInterestForecast.lowArr', $scope.PrimeInterestForecast.low);
      }else if (name == "mid"){
        $scope.PrimeInterestForecast.midCalc = PrimeInterestForecast.addToArr($scope.PrimeInterestForecast.mid, $scope.startPrime);
        $cookieStore.put('primeInterestForecast.midArr', $scope.PrimeInterestForecast.mid);
      }else if (name == "high"){
        $scope.PrimeInterestForecast.highCalc = PrimeInterestForecast.addToArr($scope.PrimeInterestForecast.high, $scope.startPrime);
        $cookieStore.put('primeInterestForecast.highArr', $scope.PrimeInterestForecast.high);
      }else if (name == "custom"){
        $scope.PrimeInterestForecast.customCalc = PrimeInterestForecast.addToArr($scope.PrimeInterestForecast.custom, $scope.startPrime);
        $cookieStore.put('primeInterestForecast.customArr', $scope.PrimeInterestForecast.custom);
      }else if("prime"){
        $cookieStore.put('primeInterestForecast.startPrime', $scope.startPrime);
        $scope.PrimeInterestForecast.lowCalc = PrimeInterestForecast.addToArr($scope.PrimeInterestForecast.low, $scope.startPrime);
        $scope.PrimeInterestForecast.midCalc = PrimeInterestForecast.addToArr($scope.PrimeInterestForecast.mid, $scope.startPrime);
        $scope.PrimeInterestForecast.highCalc = PrimeInterestForecast.addToArr($scope.PrimeInterestForecast.high, $scope.startPrime);
        $scope.PrimeInterestForecast.customCalc = PrimeInterestForecast.addToArr($scope.PrimeInterestForecast.custom, $scope.startPrime);
      }
      
      $scope.primeInterestForecastMethodChanged = function(){
        //alert($scope.selectedPrimeForecastMethod);
        if($scope.selectedPrimeForecastMethod == "edit"){
          $scope.showPrimeSpeculations = true;
        }else{
          $scope.showPrimeSpeculations = false;
          PrimeInterestForecast.setSelectedForecastMethod($scope.PrimeInterestForecast[$scope.selectedPrimeForecastMethod])
        }
      }
      /*var length = $scope.PrimeInterestForecast.low.length;
      for (var i = 0 ; i < length ; i++){
        $scope.PrimeInterestForecast.lowCalc[i] = $scope.startPrime + $scope.PrimeInterestForecast.low[i]
      }*/
    }

    $scope.month = ['ינואר', 'פברואר', 'מרץ', 'אפריל', 'מאי', 'יוני', 'יולי', 'אוגוסט', 'ספטמבר', 'אוקטובר', 'נובמבר', 'דצמבר'];
    $scope.addMonthToDate = function(months){
      var date = new Date();
      date.setDate(1);
      date.setMonth(date.getMonth() + months);
      return $scope.month[date.getMonth()] + '/' + date.getFullYear();
    };

    $scope.clearOldData = function(){
      $scope.compared = {
        our : {
          loan : 0,
          total : 0,
          payments : [],
          kerenPayments : [],
          avg : 0,
        },
        cur : {
          loan : 0,
          total : 0,
          payments : [],
          kerenPayments : [],
          avg : 0,
        },
      };
    }

    $scope.deleteRowOur = function(index){
      var tempArr = [];
      var len = $scope.ourTracks.length;
      for (var i = 0 ; i < len ; i++){
        if(i == index){
          continue;
        }else{
          tempArr.push($scope.ourTracks[i]);
        }
      }
      $scope.ourTracks = tempArr;
      $scope.compareProposals();
    }

    $scope.deleteRowCur = function(index){
      var tempArr = [];
      var len = $scope.proposedTracks.length;
      for (var i = 0 ; i < len ; i++){
        if(i == index){
          continue;
        }else{
          tempArr.push($scope.proposedTracks[i]);
        }
      }
      $scope.proposedTracks = tempArr;
      $scope.compareProposals();
    }

    $scope.sumArray = function(arr, stop){
      if(!stop){
        stop = arr.length;
      }
      var tLength = arr.length;
      var sum = 0;
      for(var i = 0 ; i < stop && i < tLength ; i++){
        sum += arr[i];
      }
      return sum;
    }

    $scope.calcMidSum = function(month){
      if(month < 1){
        return;
      }
      //document.getElementById('midSumCompareTable').scrollIntoView();
      $scope.ourMidSum = $scope.sumArray($scope.compared.our.payments, month);
      $scope.curMidSum = $scope.sumArray($scope.compared.cur.payments, month);
    }

    $scope.recalcKeren = function(sum, kerenPayments){
      var len = kerenPayments.length;
      var ret = [];
      for(var i = 0 ; i < len ; i++){
        sum -= kerenPayments[i];
        ret.push(sum);
      }
      return ret;
    }
 /*   $scope.trText = 
      "<tr>" +
        "<td>" +
          "<select class='form-control'>" +
            "<option value='0'></option>" +
            "<option value='1'>קבועה צמודה</option>" +
            "<option value='2'>פריים</option>" +
            "<option value='3'>משתנה A-1</option>" +
            "<option value='4'>משתנה A-2.5</option>" +
            "<option value='5'>משתנה A-5</option>" +
            "<option value='6'>משתנה A-10</option>" +
            "<option value='7'>משתנה B-1</option>" +
          "</select>" +
        "</td>" +
        "<td><input class='form-control' type='number'></td>" +
        "<td><input class='form-control' type='number'></td>" +
        "<td><input class='form-control' type='number'></td>" +
        "<td><div></div></td>" +
        "<td><div></div></td>" +
      "</tr>";*/
    $scope.addRowCurrentProposal = function(){
      //$('#currentProposalTable > tbody:last').append($scope.trText);
      console.log("So far proposed: " + JSON.stringify($scope.proposedTracks));
      $scope.proposedTracks.push({payMethod : "2"});
    }
    $scope.addRowOurProposal = function(){
      console.log("So far our: " + JSON.stringify($scope.ourTracks));
      $scope.ourTracks.push({payMethod : "2"});
    }

    $scope.calcAvg = function(arr){
      var length = arr.length;
      var sum = 0;
      for(var i = 0 ; i < length ; i++){
        sum += arr[i];
      }
      return Math.round(sum/length);
    }

    $scope.addSumToArray = function(target, arr){
      var length = arr.length;
      if(target.length < length){
        for(var i = target.length ; i < length ; i++){
          target[i] = 0;
        }
      }
      for(var i = 0 ; i < length ; i++){
        target[i] += arr[i];
      }
    }

    $scope.isTsmudMadad = function(track){
      return (track == MortageTypes.KVUA_TSMUDA         || 
              track == MortageTypes.MISHTANA_TSMUDA_2_5 ||
              track == MortageTypes.MISHTANA_TSMUDA_5   ||
              track == MortageTypes.MISHTANA_TSMUDA_7   ||
              track == MortageTypes.MISHTANA_TSMUDA_10);
    }

    $scope.processProposal = function(proposal, compared){
      for(var i = 0 ; i < proposal.length ; i++){
        proposal[i].color = $scope.isTsmudMadad(proposal[i].track);
        var res = RouteCalculator.calcMonthlyByRoute(proposal[i], $scope.startPrime, $scope.expectedInflationVal);
        proposal[i].monthly = Math.round(res.payments[0]);
        proposal[i].total = Math.round(res.total);
        compared.total += proposal[i].total;
        compared.loan += res.loan;
        $scope.addSumToArray(compared.payments, res.payments);
        $scope.addSumToArray(compared.kerenPayments, res.kerenPayments);
      }
      compared.avg = $scope.calcAvg(compared.payments);
      compared.ppi = (compared.total / $scope.totalLoan).toFixed(2);

    }

    $scope.compareProposals = function(){
      $scope.showCompare = true;
      $scope.clearOldData();
      /*var table_data = [];
      $scope.currTable = [];
      $('#currentProposalTable tr').each(function(){
          var row_data = [];    
          $('select', this).each(function(){
              row_data.push($(this).val());
          });    
          $('input', this).each(function(){
              row_data.push(parseInt($(this).val()));
          });    
          $('div', this).each(function(){
              row_data.push($(this));
          });
          if(row_data.length != 0){
            //table_data.push(row_data);
            $scope.currTable.push(row_data);
          }
      });

      console.log($scope.currTable);
      for(var i = 0 ; i < $scope.currTable.length ; i++){
        var res = RouteCalculator.calcMonthlyByRoute($scope.currTable[i][0], $scope.currTable[i][1], $scope.currTable[i][2], $scope.currTable[i][3], $scope.primeInterestVal, $scope.expectedInflationVal);
        console.log(res);
        $scope.currTable[i][4].html(Math.round(res.payments[0]));
        $scope.currTable[i][5].html(Math.round(res.total));
      }*/
      //document.getElementById('compareProposalsTable').scrollIntoView();
      $scope.processProposal($scope.ourTracks, $scope.compared.our);
      $scope.processProposal($scope.proposedTracks, $scope.compared.cur);
      drawCharts( $scope.compared.cur.payments, 
                  $scope.compared.our.payments, 
                  $scope.compared.cur.total, 
                  $scope.compared.our.total, 
                  $scope.compared.cur.ppi, 
                  $scope.compared.our.ppi,
                  $scope.recalcKeren($scope.compared.cur.loan, $scope.compared.cur.kerenPayments),
                  $scope.recalcKeren($scope.compared.our.loan, $scope.compared.our.kerenPayments) );
      /*for(var i = 0 ; i < $scope.ourTracks.length ; i++){
        $scope.ourTracks[i].color = $scope.isTsmudMadad($scope.ourTracks[i].track);
        var res = RouteCalculator.calcMonthlyByRoute($scope.ourTracks[i], $scope.primeInterestVal, $scope.expectedInflationVal);
        $scope.ourTracks[i].monthly = Math.round(res.payments[0]);
        $scope.ourTracks[i].total = Math.round(res.total);
        $scope.compared.our.total += $scope.ourTracks[i].total;
        $scope.addSumToArray($scope.compared.our.payments, res.payments);
      }
      for(var i = 0 ; i < $scope.proposedTracks.length ; i++){
        $scope.proposedTracks[i].color = $scope.isTsmudMadad($scope.proposedTracks[i].track);
        var res = RouteCalculator.calcMonthlyByRoute($scope.proposedTracks[i], $scope.primeInterestVal, $scope.expectedInflationVal);
        $scope.proposedTracks[i].monthly = Math.round(res.payments[0]);
        $scope.proposedTracks[i].total = Math.round(res.total);
        $scope.compared.cur.total += $scope.proposedTracks[i].total;
        $scope.addSumToArray($scope.compared.cur.payments, res.payments);
      }
      $scope.compared.cur.avg = $scope.calcAvg($scope.compared.cur.payments);
      $scope.compared.our.avg = $scope.calcAvg($scope.compared.our.payments);
      $scope.compared.cur.ppi = ($scope.compared.cur.total / $scope.totalLoan).toFixed(2);
      $scope.compared.our.ppi = ($scope.compared.our.total / $scope.totalLoan).toFixed(2);*/
    }
}